﻿// CWorkerDlg.cpp: 实现文件
//

#include "pch.h"
#include "CClient.h"
#include "CWorkerDlg.h"
#include "afxdialogex.h"
#include "CInputDlg.h"
#include "CModifyDlg.h"


// CWorkerDlg 对话框

IMPLEMENT_DYNAMIC(CWorkerDlg, CDialogEx)

CWorkerDlg::CWorkerDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_WORKER, pParent)
{

}

CWorkerDlg::~CWorkerDlg()
{
}

void CWorkerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_ITEM, m_edit_item);
	DDX_Control(pDX, IDC_COMBO_ITEM, m_combo_item);
}


BEGIN_MESSAGE_MAP(CWorkerDlg, CDialogEx)
	ON_NOTIFY(HDN_ITEMCLICK, 0, &CWorkerDlg::OnHeadItemclick)
	ON_BN_CLICKED(IDC_WORKER_DEL, &CWorkerDlg::OnClickedDel)
	ON_BN_CLICKED(IDC_WORKER_ADD, &CWorkerDlg::OnClickedAdd)
	ON_BN_CLICKED(IDC_WORKER_MODIFY, &CWorkerDlg::OnClickedModify)
	ON_NOTIFY(NM_DBLCLK, IDC_WORKER_LIST, &CWorkerDlg::OnDblclkWorkerList)
	ON_EN_KILLFOCUS(IDC_EDIT_ITEM, &CWorkerDlg::OnKillfocusEditItem)
END_MESSAGE_MAP()


// CWorkerDlg 消息处理程序


BOOL CWorkerDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 启动时隐藏两个控件
	m_edit_item.ShowWindow(SW_HIDE);

	auto pList = (CListCtrl*)GetDlgItem(IDC_WORKER_LIST);
	pList->InsertColumn(0, _T("工号"), LVCFMT_LEFT, 120);
	pList->InsertColumn(1, _T("姓名"), LVCFMT_LEFT, 120);
	pList->InsertColumn(2, _T("工资"), LVCFMT_LEFT, 120);
	pList->InsertColumn(3, _T("入职日期"), LVCFMT_LEFT, 150);
	// 设置风格 网格 全行选中
	pList->SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
	Refresh();

	return FALSE;
}

void CWorkerDlg::Refresh(int order)
{
	int cmd = WK_PRINT;
	// socket发生指令和排序
	theApp.m_sock.Send(&cmd, sizeof(cmd));
	theApp.m_sock.Send(&order, sizeof(order));

	auto pList = (CListCtrl*)GetDlgItem(IDC_WORKER_LIST);
	pList->DeleteAllItems();
	SInfo d;
	CString str;
	int i = 0;
	while (theApp.m_sock.Receive(&d, sizeof(d)) == sizeof(d)) {
		// int -> CString
		str.Format(_T("%d"), d.nNumb);
		pList->InsertItem(i, str);
		pList->SetItemText(i, 1, theApp.UTF8ToUnicode(d.sName));
		str.Format(_T("%g"), d.fSala);
		pList->SetItemText(i, 2, str);
		pList->SetItemText(i, 3, theApp.UTF8ToUnicode(d.sDate));
		i++;
	}
}

// 获取每行的String
CString CWorkerDlg::GetLine(int index)
{
	auto pList = (CListCtrl*)GetDlgItem(IDC_WORKER_LIST);
	if (index >= pList->GetItemCount()) {
		return CString();
	}
	int columns = pList->GetHeaderCtrl()->GetItemCount();
	CString str;
	for (int i = 0; i < columns; i++) {
		str += pList->GetItemText(index, i) + _T(", ");
	}
	return str;
}


void CWorkerDlg::OnHeadItemclick(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);
	int nOrder = phdr->iItem;// 第几列
	Refresh(nOrder);

	*pResult = 0;
}


void CWorkerDlg::OnClickedDel()
{
	auto pList = (CListCtrl*)GetDlgItem(IDC_WORKER_LIST);
	POSITION pos = pList->GetFirstSelectedItemPosition();
	if (!pos) {
		MessageBox(_T("未选中行"), _T("提示"));
		return;
	}
	while (pos) {
		int i = pList->GetNextSelectedItem(pos);
		CString str = GetLine(i);
		if (MessageBox(str + _T("删除吗？"), _T("提示"), MB_YESNO) == IDYES) {
			int cmd = WK_DELETE;
			int num = _tstoi(pList->GetItemText(i, 0));// atoi 字符串转int
			theApp.m_sock.Send(&cmd, sizeof(cmd));
			theApp.m_sock.Send(&num, sizeof(num));
			if (theApp.m_sock.Receive(&num, sizeof(num)) != sizeof(num)) {
				MessageBox(_T("网络异常"), _T("提示"));
				return;
			}
			if (num < 1) {
				MessageBox(_T("删除失败"), _T("提示"));
				return;
			}
		}
	}
	Refresh();
}


void CWorkerDlg::OnClickedAdd()
{
	CInputDlg dlg(this, this);
	dlg.DoModal();
}


void CWorkerDlg::OnClickedModify()
{
	auto pList = (CListCtrl*)GetDlgItem(IDC_WORKER_LIST);
	POSITION pos = pList->GetFirstSelectedItemPosition();
	if (!pos) {
		MessageBox(_T("未选中行"), _T("提示"));
		return;
	}
	int nItem = pList->GetNextSelectedItem(pos);
	CModifyDlg dlg;
	dlg.num = pList->GetItemText(nItem, 0);
	dlg.name = pList->GetItemText(nItem, 1);
	dlg.salary = pList->GetItemText(nItem, 2);
	dlg.date = pList->GetItemText(nItem, 3);
	if (dlg.DoModal() == IDOK) {// 返回值来自EndDialog
		Refresh();
	}
}

// list列表的双击 属性 编辑标签：TRUE  
void CWorkerDlg::OnDblclkWorkerList(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;

	auto pList = (CListCtrl*)GetDlgItem(IDC_WORKER_LIST);
	// 有局限只能编辑第一列
	// pList->EditLabel(pNMItemActivate->iItem);

	int line = pNMItemActivate->iItem;
	// 第几列
	int column = pNMItemActivate->iSubItem;
	CRect rect;
	if (column == 1) {
		pList->GetSubItemRect(line, column, 0, rect);
		m_edit_item.SetParent(pList);
		m_edit_item.MoveWindow(rect, FALSE);
		m_edit_item.ShowWindow(SW_SHOW);
		CString str = pList->GetItemText(line, column);
		m_edit_item.SetWindowText(str);
		m_edit_item.SetFocus();
		m_edit_item.SetSel(0, -1);// 全选
		// 设置隐藏数据
		SetWindowLongPtr(m_edit_item.GetSafeHwnd(), GWLP_USERDATA, line);
	}
}


// 虚函数 ESC或关闭时调用
void CWorkerDlg::OnCancel()
{
	if (m_edit_item.IsWindowVisible()) {
		m_edit_item.ShowWindow(SW_HIDE);
		m_combo_item.ShowWindow(SW_HIDE);
		return;
	}
	
	CDialogEx::OnCancel();
}

// 虚函数 回车或OK时调用
void CWorkerDlg::OnOK()
{
	if (m_edit_item.IsWindowVisible()) {
		m_edit_item.ShowWindow(SW_HIDE);
		m_combo_item.ShowWindow(SW_HIDE);
		// 64位 GetWindowLongPtr 
		int line = GetWindowLongPtr(m_edit_item.GetSafeHwnd(), GWLP_USERDATA);
		return;
	}

	CDialogEx::OnOK();
}

// 失去焦点监听
void CWorkerDlg::OnKillfocusEditItem()
{
	TRACE(_T("------>OnKillfocusEditItem\n"));
	if (m_edit_item.IsWindowVisible()) {
		m_edit_item.ShowWindow(SW_HIDE);
		m_combo_item.ShowWindow(SW_HIDE);
		// 64位 GetWindowLongPtr 
		int line = GetWindowLongPtr(m_edit_item.GetSafeHwnd(), GWLP_USERDATA);
	}
}
